package com.dhecp.project.system.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.dhecp.core.layui.GridColumnsPropertyModel;
import com.dhecp.core.utils.ExportExcelUtils;
import com.dhecp.project.system.service.SysDictionaryForFieldService;
import com.dhecp.project.system.service.SysUserBookService;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.dhecp.common.annotation.Log;
import com.dhecp.common.annotation.RepeatSubmit;
import com.dhecp.common.core.controller.BaseController;
import com.dhecp.common.core.domain.AjaxResult;
import com.dhecp.common.core.page.TableDataInfo;
import com.dhecp.common.enums.BusinessType;
import com.dhecp.common.utils.TypeUtils;
import com.dhecp.core.constant.ModuleTable;
import com.dhecp.project.common.service.ButtonStateService;
import com.dhecp.project.system.domain.SysDictionaryForExport;
import com.dhecp.project.system.domain.SysUserBook;
import org.springframework.web.servlet.ModelAndView;

/**
 * 员工通讯录 控制层
 * @author DH-Xxj
 * @version 2019年10月10日
 */
@Controller
@RequestMapping("/system/userbook")
public class SysUserBookController extends BaseController{
//    private String prefix = "system/userbook";
    private String moduleTable = ModuleTable.USER_BOOK;

    @Autowired
    private SysUserBookService userBookService;

    @Autowired
    private GridColumnsPropertyModel gridColumnsPropertyModel;
    
    @Autowired
    private SysDictionaryForFieldService dictionaryForFieldService;
	
	@Autowired
	private ButtonStateService buttonStateService;

    /**
     * 打开员工通讯录
     * @param 
     * @return ModelAndView
     * @author DH-Xxj
     */
    @RequiresPermissions("system:userbook:view")
    @GetMapping()
    public ModelAndView userInfoList(@RequestParam Map<String, Object> params){
    	params.put("moduleTable", moduleTable);
    	params.put("entity", new SysUserBook()); //生成表格gridColumns对应的实体，用于获取实体属性生成表格字段    	
    	params.put("toolBarWidth", 200);
    	
        ModelAndView mv = this.getModelAndView();
        Map<String, Object> buttonState = new HashMap<String, Object>();
        buttonState = buttonStateService.getButtonStateObjectForUser(params);
        
        mv.addObject("gridColumns", gridColumnsPropertyModel.queryGridColumnsPropertyModel(params)); 
        mv.addObject("moduleTable", moduleTable); 
        mv.addObject("buttonState", TypeUtils.mapToJson(buttonState));		//按钮状态：（可用：enable，禁用：disable，隐藏：hidden）
        mv.addObject("hr", "userbook");
        mv.setViewName("hr/hr");
        return mv;
    }

    /**
     * 按条件查询员工通讯录
     * @param 
     * @return GridDataModel
     * @throws Exception
     * @author DH-Xxj
     */
    @RequiresPermissions("system:userbook:list")
    @GetMapping("/list")
    @ResponseBody
    public TableDataInfo userBookListJson(@RequestParam Map<String, Object> params){
    	params.put("moduleTable", moduleTable);
        try {
            List<SysUserBook> list = new ArrayList<SysUserBook>();
    		list = userBookService.queryUserBookList(params);
            return getDataTable(list);
        } catch (Exception e) {
            e.printStackTrace();
            return getDataTable(this.getExceptionMessage(e).toString());
        }
    }

    /**
     * 数据导出Excel
     * @param params
     * @return
     * @throws Exception
     * @author DH-Xxj
     */
    @RequiresPermissions("system:userbook:export")
    @Log(title = "员工通讯录导出", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult export(@RequestParam Map<String, Object> params) throws Exception {
		params.put("moduleTable", moduleTable);
    	params.put("entity", new SysUserBook()); 
    	
        List<SysUserBook> list = userBookService.queryUserBookList(params);
        
    	params.put("getFieldAttributesType", "export");
    	List<SysDictionaryForExport> fieldList = dictionaryForFieldService.queryExportFieldList(params);
        ExportExcelUtils<SysUserBook> util = new ExportExcelUtils<SysUserBook>(moduleTable,new SysUserBook());
        return util.exportExcel(list, fieldList, "员工通讯录", "export");
    }

    /**
     * 修改员工备注信息
     * @param userId 必要参数：用户编号
     * @param vBzXx 必要参数：备注信息
     * @return
     * @author DH-Xxj
     */
    @RequiresPermissions("system:userbook:edit")
    @Log(title = "修改员工备注信息", businessType = BusinessType.UPDATE)
    @PostMapping("/editpost")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult updateBzXxPost(@RequestParam Map<String, Object> params) {
        AjaxResult ajaxResult = new AjaxResult();
        if (params.get("userId") == null || params.get("userId") == ""){
        	return AjaxResult.error("缺少参数userId【用户编号】。");
        }
        if (params.get("vBzXx") == null || params.get("vBzXx") == ""){
        	return AjaxResult.error("缺少参数vBzXx【备注信息】。");
        }
        try {
            ajaxResult= userBookService.updateBzXx(params);
        }catch (Exception e) {
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        return ajaxResult;
    }

}